#ifndef FREYACOMMON_H
#define FREYACOMMON_H

#include <QString>
#include <QVariant>
#include <QDateTime>
#include <QStandardPaths>

#include "freyacommon_global.h"

#define FREYA_DATAOPERATE(OpFunc, OpArgs, OpResult, OpRet)  \
                                                        { FreyaData pResultData = FreyaBaseData::CreateData(FGP_CMD_DATAOPERATE); \
                                                        DataOperate dataOpera; \
                                                        dataOpera.OperaFunc = OpFunc; \
                                                        dataOpera.OperaArgs = OpArgs; \
                                                        pResultData->SetArgument(dataOpera.ToVariant()); \
                                                        if(FREYA_QUERYEXECUTION(pResultData)) { \
                                                            dataOpera.FromVariant(pResultData->GetArgument());\
                                                            OpResult = dataOpera.OperaResult; \
                                                            OpRet = dataOpera.OperaRet; } \
                                                        else { qWarning() << "FREYA_DATAOPERATE opera failed:" << OpFunc << OpArgs; } }

#define SYSLOGRECORD(logString, orgFreyaData)  { FreyaData sysLogData = FreyaBaseData::CreateData(FGP_CMD_SYSTEMLOGRECORD); \
                                                    SystemLog logData; \
                                                    logData.LogString = logString; \
                                                    if(!orgFreyaData.isNull()) { \
                                                    logData.FreyaSerializeData = FreyaCommon::ByteToString(FreyaBaseData::Serialize(orgFreyaData)); } \
                                                    sysLogData->SetArgument(logData.ToVariant()); \
                                                    FREYA_REQUESTEXECUTION(sysLogData); }

// path
#define PATH_APPLICATION             QCoreApplication::applicationDirPath()
#define PATH_WRITABLE                QStandardPaths::writableLocation(QStandardPaths::DataLocation)

// log path
#define PATH_LOG                     PDFPATH_WRITABLE+"/log/"

// define
#define FMT_DATETIME                    "yyyy-MM-ddTHH:mm:ss"

enum ComAck
{
    e_AckTimeout    = -2,   // 状态超时
    e_AckError      = -1,   // 状态错误
    e_AckUnknow     = 0,    // 状态未知
    e_AckOk         = 1     // 状态正常
};

enum ComRep
{
    e_RepTimeout    = -1,   // 等待超时
    e_RepUnknow     = 0,    // 未知上报
    e_RepOk         = 1,    // 正常回复
    e_RepErr        = 2,    // 上报错误
    e_RepAct        = 3     // 主动上报
};

enum DetectOperate
{
    e_UnknowDetect = 0,
//    e_Start,
//    e_Append,
    e_StopDetect,
    e_PauseDetect,
    e_ResumeDetect
};

namespace FreyaCommon
{
FRYCOMMON_EXPORT QString CommonVersion();

FRYCOMMON_EXPORT QString DateTimeToString(const QDateTime &dt);
FRYCOMMON_EXPORT QDateTime StringToDateTime(const QString &st);

FRYCOMMON_EXPORT QString ByteToString(const QByteArray &ba);

FRYCOMMON_EXPORT QByteArray StringToByte(const QString &str);

FRYCOMMON_EXPORT unsigned int Crc_32(QByteArray &ba, unsigned int crc = 0);
}

#endif // FREYACOMMON_H
